﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.BasicControl;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel.Data;
using Xinkui.WebModel.Cache;
using CommonFramework.Utility;
using Xinkui.WebModel;
public partial class UserControl_ShowRoom_UC_Right_ProductListForImage : ShowRoomControl {

    protected int _pageSIze = 8;
    public int PageSIze {
        get {
            return _pageSIze;
        }
        set {
            if (_pageSIze == value)
                return;
            _pageSIze = value;
        }
    }

    public string ListURL { get; set; }

    public string GarllyURL { get; set; }


    bool IsSearching {
        get {

            return !string.IsNullOrEmpty(ShowRoomQueryData.Create().Keyword);
        }
    }

    protected void Page_Load(object sender, EventArgs e) {
        var queryData = ShowRoomQueryData.Create();
        int totalCount = 0;
        int fiterCategory = queryData.CategoryLevel3 > 0 ? queryData.CategoryLevel3 : (queryData.CategoryLevel2 > 0 ? queryData.CategoryLevel2 : queryData.CategoryLevel1);

        //if (fiterCategory == 0) {
        //    var allCategory = CategoryCache.ListCategoryByCompany(this.BasePage.ShowRoomIdentity);

        //    var level1 = allCategory.FirstOrDefault();

        //    if (level1 != null) {
        //        fiterCategory = level1.SubViews.Select(u => u.CategoryInfo.CategoryID).FirstOrDefault();
        //    }

        //}
        List<string> categoryIds = new List<string>(3);
        categoryIds.Add(queryData.CategoryLevel1.ToString());
        categoryIds.Add(queryData.CategoryLevel2.ToString());
        categoryIds.Add(queryData.CategoryLevel3.ToString());
        GarllyURL = URLHelper.LinkToShowRoom_ProductListForImage(BasePage.ShowRoomIdentity, categoryIds, queryData.Keyword);
        ListURL = URLHelper.LinkToShowRoom_ProductListForList(BasePage.ShowRoomIdentity, categoryIds, queryData.Keyword);

        if (!IsSearching) {
            this.rptProductDataSource.DataSource = Singleton<ProductInfoDataAccess>.Instance.ListShowRoomProducts(queryData.PageIndex, PageSIze, this.BasePage.ShowRoomIdentity, fiterCategory, out totalCount);
        }
        else {
            this.rptProductDataSource.DataSource = Singleton<ProductInfoDataAccess>.Instance.SearchShowRoomProducts(queryData.Keyword, queryData.PageIndex, PageSIze, this.BasePage.ShowRoomIdentity, fiterCategory, out totalCount);
        }
        this.rptProductDataSource.DataBind();
        UC_RewritePager1.FormatUrl = true;
        UC_RewritePager1.CoverNav = false;

        this.UC_RewritePager1.NoteTotal = totalCount;
        this.UC_RewritePager1.JumpUrl = GarllyURL + "&p={0}";//"ProductListForImage.aspx?com=1&p={0}";//JumpUrl;
        this.UC_RewritePager1.PageIndex = queryData.PageIndex;
        this.UC_RewritePager1.PageSize = PageSIze;
        this.UC_RewritePager1.PageStep = 5;

    }
}